<template>
  <div>
    <el-dialog title="新增员工" :visible="showDialog" @close="btnCancle">
      <!-- 中间部分 -->
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="姓名:" prop="username">
          <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机:" prop="mobile">
          <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="入职时间:" prop="timeOfEntry">
          <el-date-picker
            v-model="formData.timeOfEntry"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="聘用形式:" prop="formOfEmployment">
          <el-select v-model="formData.formOfEmployment" placeholder="请选择">
            <el-option
              v-for="item in EmployeeEnum.hireType"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工号:" prop="workNumber">
          <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
        </el-form-item>
        <el-form-item label="部门:" prop="departmentName">
          <el-input v-model="formData.departmentName" style="width:50%" placeholder="请输入部门" @focus="getDepartments" />
          <!-- 放置一个树形组件 -->
          <el-tree v-if="showTree" v-loading="loading" :data="treeData" :props="{label:'name'}" :default-expand-all="true" @node-click="selectNode" />
        </el-form-item>
        <el-form-item label="转正时间:" prop="correctionTime">
          <el-date-picker
            v-model="formData.correctionTime"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
      </el-form>
      <!-- 底部按钮部分 -->
      <template v-slot:footer>
        <el-row type="flex" justify="center" align="middle">
          <el-col :span="6">
            <el-button @click="btnCancle">取消</el-button>
            <el-button type="primary" @click="btnOk">提交</el-button>
          </el-col>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import EmployeeEnum from '@/api/constant/employees'
import { addEmployee } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      EmployeeEnum,
      // 定义表单数据
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          // pattern正则表达式如果满足就通过校验，如果不满足则不通过
          pattern: /^1[3-9]\d{9}$/, message: '您的手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        // 这里为什么要用change
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      },
      options: [],
      treeData: [], // 需要定一个数组来接受树形结构
      showTree: false, // 默认不显示树形组件
      loading: false // 加上一个进度条
    }
  },
  methods: {
    async getDepartments() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      // depts是一个数组要转化成树形结构,才可以被el-tree显示
      this.treeData = tranListToTreeData(depts, '')
      this.loading = false
    },
    // 树形组件被点击时的回调
    selectNode(node) {
      this.formData.departmentName = node.name
      this.showTree = false
    },
    async btnOk() {
      // 校验表单
      // try,cacth来校验promise对象用await接受后的结果(再一次的preomise对象)
      try {
        await this.$refs.formRef.validate()
        // 校验成功
        await addEmployee(this.formData)
        this.$emit('addsuccess', false)
        // 这里不用写重置因为关闭弹层出发了close事件
      } catch (error) {
        console.log(error)
      }
    },
    btnCancle() {
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$refs.formRef.resetFields()
      this.$emit('cancelSuccess', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
